package com.axapoint.opoc.core.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.joda.time.DateTime;

import com.anasoft.os.daofusion.bitemporal.WrappedBitemporalProperty;
import com.anasoft.os.daofusion.bitemporal.WrappedValueAccessor;

@Entity
@Table(name = "CprIntervalType")
public class CprInterval extends ObjectRegistration<CprInterval, CprInterval.Value, CprInterval.Bitemporal> {
	private static final long serialVersionUID = 9168710745762860711L;

	@Entity
	@Table(name = "CprIntervalValues")
	public static final class Value extends _Value<CprInterval, Bitemporal> {
		private static final long serialVersionUID = 1L;
		private String intervalType;
		private String intervalMin;
		private String intervalMax;

		protected Value() {};
		Value(CprInterval et, String systemId, String userId, Operation operation, String comment, Value value) {
			super(et, systemId, userId, operation, comment);
			this.intervalType = value.intervalType;			
			this.intervalMin = value.intervalMin;			
			this.intervalMax = value.intervalMax;			
		}
		public Value(String intervalType, String intervalMin, String intervalMax) {
			this.intervalType = intervalType;			
			this.intervalMin = intervalMin;			
			this.intervalMax = intervalMax;			
		}
		public String getIntervalType() {
			return intervalType;
		}
		protected void setIntervalType(String intervalType) {
			this.intervalType = intervalType;
		}
		public String getIntervalMin() {
			return intervalMin;
		}
		protected void setIntervalMin(String intervalMin) {
			this.intervalMin = intervalMin;
		}
		public String getIntervalMax() {
			return intervalMax;
		}
		protected void setIntervalMax(String intervalMax) {
			this.intervalMax = intervalMax;
		}
		@Override
		public String diff(_Value<CprInterval, Bitemporal> otherValue, DateTime t0, DateTime t1) {
			StringBuffer sb = new StringBuffer();
			Value other = (Value)otherValue;
			
			if (intervalType != null && !intervalType.equals(other.intervalType)) {
				sb.append("Interval.type: ").append(intervalType).append(" - ").append(other.intervalType).append('\n');
			}
			if (intervalMin != null && !intervalMin.equals(other.intervalMin)) {
				sb.append("Interval.min: ").append(intervalMin).append(" - ").append(other.intervalMin).append('\n');
			}
			if (intervalMax != null && !intervalMax.equals(other.intervalMax)) {
				sb.append("Interval.max: ").append(intervalMax).append(" - ").append(other.intervalMax).append('\n');
			}
			
			return sb.toString();
		}
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append(super.toString());
			sb.append("  type=").append(intervalType).append('\n');
			sb.append("  min=").append(intervalMin).append('\n');
			sb.append("  maxe=").append(intervalMax).append('\n');

			int cnt = 0;
			for (Engagement.Value v : getEntity().getEngagements()) {
				if (v.getBitemporal().getValidityInterval().contains(getBitemporal().getValidityInterval().getStart())) {
					cnt++;
				}
			}
			sb.append("  #engagements=").append(cnt).append('\n');			
			
			return sb.toString();
		}
	}
	
	@Entity
	@Table(name = "CprIntervalBitemporal")
	public static final class Bitemporal extends _Bitemporal<Bitemporal, Value> {
		private static final long serialVersionUID = -2132187534862370208L;
		
		protected Bitemporal() {}
	    private Bitemporal(CprInterval.Value value, org.joda.time.Interval validityInterval) {
	        super(value, validityInterval);
	    }
	    
	    @Override
	    public Bitemporal copyWith(org.joda.time.Interval validityInterval) {
	    	return new Bitemporal(value, validityInterval);
	    }
	}

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="cprIntervalId")
    private Collection<Engagement.Value> engagements = new LinkedList<Engagement.Value>();	

	public CprInterval() {
		super();
	}
	public Collection<Engagement.Value> getEngagements() {
		return engagements;
	}
	protected void setEngagements(Collection<Engagement.Value> engagements) {
		this.engagements = engagements;
	}
    public WrappedBitemporalProperty<Value, Bitemporal> getValues() {
    	return new WrappedBitemporalProperty<Value, Bitemporal>(values, new WrappedValueAccessor<Value,Bitemporal>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Bitemporal wrapValue(Value value, org.joda.time.Interval validityInterval) {
				Bitemporal bi = new Bitemporal(value, validityInterval);
				return bi;
			}
    	});
    }
}
